package design_patterns.建造者模式;

public class HttpRequest {
    // 通过静态内部内Builder 创建对象的时候更加的灵活
    private String url;
    private String method;
    private String body;

    @Override
    public String toString() {
        return "HttpRequest{" +
                "url='" + url + '\'' +
                ", method='" + method + '\'' +
                ", body='" + body + '\'' +
                '}';
    }

    public HttpRequest(Builder builder) {
        this.url = builder.url;
        this.method = builder.method;
        this.body = builder.body;
    }

    public static class Builder {
        private String url;
        private String method;
        private String body;

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder setMethod(String method) {
            this.method = method;
            return this;
        }

        public Builder setBody(String body) {
            this.body = body;
            return this;
        }

        public HttpRequest build() {
            return new HttpRequest(this);
        }
    }

    public static void main(String[] args) {
        Builder builder = new Builder();
        builder.setUrl("http://www.baidu.com");
        builder.setMethod("GET");
        builder.setBody(null);
        HttpRequest request = builder.build();
        System.out.println(request.toString());

        HttpRequest build = new Builder().build();
    }
}